import { defineStore } from 'pinia'

import { uniStorage } from '@/utils/storage'
interface UserState {
  token: string
  nickName: string
  avatarUrl: string
  // 这样写很不规范
  [name: string]: string
}

export const useUserStore = defineStore('userInfo', {
  state: (): UserState => ({
    token: 'cs123' || '',
    avatarUrl: '',
    nickName: '我是测试名',
  }),
  actions: {
    setUserInfo(userInfo: UserState) {
      this.nickName = userInfo.nickName
      this.avatarUrl = userInfo.avatarUrl
    },
  },
  // 持久化插件
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'userInfo',
        storage: uniStorage,
      },
    ],
  },
})
